<template>
  <view class="container">
    <!-- 页面标题 -->
    <view class="page-header">
      <view class="header-content">
        <text class="page-title">全部类目</text>
      </view>
    </view>

    <!-- 大类目列表 -->
    <view class="category-section" v-for="(parentCategory, index) in parentCategories" :key="index">
      <view class="section-title"  @click="goToCategory(parentCategory)">{{ parentCategory.categoryName }}</view>
      
      <!-- 小类目标签 -->
      <view class="category-tabs">
          <view
            class="tab-item"
            v-for="(childCategory, childIndex) in getChildCategories(parentCategory.categoryId)" 
            :key="childIndex"
            @click="goToCategoryDetail(childCategory)"
          >
            <view class="tab-icon" v-if="childCategory.iconUrl">
              <image class="icon-image" :src="childCategory.iconUrl" mode="aspectFit"></image>
            </view>
            <text class="tab-name">{{ childCategory.categoryName }}</text>
          </view>
      </view>
    </view>
    
    <!-- 空状态 -->
    <view class="empty-state" v-if="parentCategories.length === 0 && !loading">
      <u-empty 
        mode="list" 
        text="暂无类目数据" 
        icon="http://cdn.uviewui.com/uview/empty/list.png"
        marginTop="100"
      >
      </u-empty>
    </view>
    
    <!-- 加载状态 -->
    <view class="loading-state" v-if="loading">
      <u-loading-icon mode="circle" size="28" text="加载中..."></u-loading-icon>
    </view>
  </view>
</template>

<script>
import config from '@/common/config.js';

export default {
  data() {
    return {
      allCategories: [], // 存储所有类目数据
      parentCategories: [], // 存储大类目数据
      loading: true,
      defaultIcon: '/static/category/default.png'
    };
  },
  
  onLoad() {
    this.loadCategories();
  },
  
  methods: {
    goToCategory(item) {
      console.log('跳转到分类:', item)
      uni.navigateTo({
        url: `/pages/question/index?parentId=${item.categoryId}`
      })
      // 跳转到分类页面
    },
    // 加载所有类目
    loadCategories() {
      this.loading = true;
      this.$request('/interview/category/list', 'GET')
        .then(res => {
          if (res.code === 200) {
            // 处理所有类目数据
            this.allCategories = res.rows.map(item => {
              return {
                ...item,
                iconUrl: item.icon ? `${item.icon}` : ''
              };
            });
            
            // 筛选出大类目（parentId为0或null的类目）
            this.parentCategories = this.allCategories.filter(category => 
              !category.parentId || category.parentId === 0
            );
          } else {
            uni.showToast({
              title: '获取类目失败',
              icon: 'none'
            });
          }
        })
        .catch(err => {
          console.error('获取类目异常:', err);
          uni.showToast({
            title: '网络异常',
            icon: 'none'
          });
        })
        .finally(() => {
          this.loading = false;
        });
    },
    
    // 获取指定大类目的子类目
    getChildCategories(parentId) {
      return this.allCategories.filter(category => 
        category.parentId === parentId
      );
    },
    
    // 跳转到类目详情页
    goToCategoryDetail(category) {
      uni.navigateTo({
        url: `/pages/question/index?categoryId=${category.categoryId}&categoryName=${encodeURIComponent(category.categoryName)}`
      });
    }
  }
};
</script>

<style scoped>
.container {
  min-height: 100vh;
  background: url("data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIiBmaWxsPSJub25lIiB2ZXJzaW9uPSIxLjEiIHdpZHRoPSIxMjY3IiBoZWlnaHQ9IjkxOSIgdmlld0JveD0iMCAwIDEyNjcgOTE5Ij48ZGVmcz48bGluZWFyR3JhZGllbnQgeDE9IjAuODUxMjA2NDgxNDU2NzU2NiIgeTE9IjAuOTI1NjAzMTUxMzIxNDExMSIgeDI9IjAuMzA2NjA3NDUxMjE3NTkwMzciIHkyPSIwLjc1NjY1OTIyODgyNTk1NjUiIGlkPSJtYXN0ZXJfc3ZnMF85XzAzNyI+PHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI0Y0RjVGQSIgc3RvcC1vcGFjaXR5PSIxIi8+PHN0b3Agb2Zmc2V0PSIxMDAlIiBzdG9wLWNvbG9yPSIjRjRGNUZBIiBzdG9wLW9wYWNpdHk9IjAiLz48L2xpbmVhckdyYWRpZW50PjxsaW5lYXJHcmFkaWVudCB4MT0iMC44NTEyMDY0ODE0NTY3NTY2IiB5MT0iMC45MjU2MDMxNTEzMjE0MTExIiB4Mj0iLTEuMTg1MDY5MzcxNTY2MDg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SBjeD0iMzMzIiBjeT0iNzUwIiByeD0iMzUiIHJ5PSIzNSIgZmlsbD0idXJsKCNtYXN0ZXJfc3ZnMV85XzA0MCkiIGZpbGwtb3BhY2l0eT0iMSIvPjwvZz48ZyB0cmFuc2Zvcm09Im1hdHJpeCgxLDAsMCwtMSwwLDE3MTQpIj48ZWxsaXBzZSBjeD0iNjA4LjUiIGN5PSI4ODEuNSIgcng9IjI0LjUiIHJ5PSIyNC41IiBmaWxsPSJ1cmwoI21hc3Rlcl9zdmcyXzlfMDMzKSIgZmlsbC1vcGFjaXR5PSIxIi8+PC9nPjxnIHRyYW5zZm9ybT0ibWF0cml4KDEsMCwwLC0xLDAsMTc4MikiPjxlbGxpcHNlIGN4PSIxODUiIGN5PSI5MDgiIHJ4PSIxNyIgcnk9IjE3IiBmaWxsPSJ1cmwoI21hc3Rlcl9zdmczXzlfMDMzKSIgZmlsbC1vcGFjaXR5PSIxIi8+PC9nPjxnIHRyYW5zZm9ybT0ibWF0cml4KDEsMCwwLC0xLDAsMTU0NikiPjxlbGxpcHNlIGN4PSIxMDMzIiBjeT0iNzkwIiByeD0iMTciIHJ5PSIxNyIgZmlsbD0idXJsKCNtYXN0ZXJfc3ZnNF85XzAzMykiIGZpbGwtb3BhY2l0eT0iMSIvPjwvZz48ZyB0cmFuc2Zvcm09Im1hdHJpeCgxLDAsMCwtMSwwLDEyOTApIj48ZWxsaXBzZSBjeD0iODgzLjUiIGN5PSI2NzkuNSIgcng9IjM0LjUiIHJ5PSIzNC41IiBmaWxsPSJ1cmwoI21hc3Rlcl9zdmc1XzlfMDMzKSIgZmlsbC1vcGFjaXR5PSIxIi8+PC9nPjwvZz48L3N2Zz4=");
  background-position-y: -300rpx;
  background-size: contain;
  background-position-x: 100rpx;
  background-repeat: no-repeat;
  background-color: #ffffff;
  padding-top: 90px;
  padding-bottom: 40px;
}

.page-header {
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 20rpx;
}

.header-content {
  text-align: center;
}

.page-title {
  font-size: 36rpx;
  font-weight: 500;
  color: var(--primary-dark-color);
}

.category-section {
  border-radius: 20rpx;
  padding: 20rpx;
  margin-bottom: 20rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: var(--primary-dark-color);
  margin-bottom: 15px;
  position: relative;
  padding-left: 12px;
}

.section-title::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 5px;
  height: 22px;
  background: linear-gradient(to bottom, var(--primary-dark-color), var(--primary-color));
  border-radius: 3px;
}

.category-tabs {
  white-space: nowrap;
  width: 100%;
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  align-content: center;
  align-items: center;
}

.category-tabs-wrapper {
  display: inline-block;
  width: max-content;
  padding: 10px 0;
}

.tab-item {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  padding: 15rpx 20rpx;
  margin-right: 20rpx;
  background: rgba(255, 255, 255, 0.7);
  border-radius: 10rpx;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: all 0.3s;
  min-width: 100rpx;
  text-align: center;
  margin-top: 10px;
  /* 毛玻璃效果 */
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  /* 添加边框以增强效果 */
  border: 1px solid rgba(255, 255, 255, 0.4);
}

.tab-item:hover {
  background: rgba(230, 247, 255, 0.8);
  transform: translateY(-4rpx);
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
}

.tab-icon {
  width: 50rpx;
  height: 50rpx;
  margin-bottom: 10rpx;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-image {
  width: 100%;
  height: 100%;
}

.tab-name {
  font-size: 24rpx;
  color: #333;
  display: block;
}

.empty-state, .loading-state {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 60vh;
}